package com.example.screenshoot;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ListView list = (ListView) findViewById(R.id.listMess);
		ArrayList<Message> array = new ArrayList<Message>();
		ListViewAdapter arrayAdapter = new ListViewAdapter(this, R.layout.listview, BOSMS.getAllSms(this));
		list.setAdapter(arrayAdapter);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	     switch (keyCode) {
	     case KeyEvent.KEYCODE_VOLUME_DOWN:
	    	 Bitmap bitmap = takeScreenshot();
		       saveBitmap(bitmap,"volumn down");
	        return true;
	     }
	     return false;
	}
	private void saveBitmap(Bitmap bitmap,String fileName) {
		  File imagePath = new File(Environment.getExternalStorageDirectory() + "/"+fileName+".png");
		    FileOutputStream fos;
		    try {
		        fos = new FileOutputStream(imagePath);
		        bitmap.compress(CompressFormat.JPEG, 100, fos);
		        fos.flush();
		        fos.close();
		    } catch (FileNotFoundException e) {
		        Log.e("GREC", e.getMessage(), e);
		    } catch (IOException e) {
		        Log.e("GREC", e.getMessage(), e);
		    }
	}

	private Bitmap takeScreenshot() {
		View rootView = findViewById(android.R.id.content).getRootView();
		   rootView.setDrawingCacheEnabled(true);
		   return rootView.getDrawingCache();
	}
	@Override
    protected void onPause() {
        // WHEN THE SCREEN IS ABOUT TO TURN OFF
		 Bitmap bitmap = takeScreenshot();
	       saveBitmap(bitmap,"turnof");
	       Log.e("GREC", ScreenReceiver.wasScreenOn+"");
        super.onPause();
    }
 
    @Override
    protected void onResume() {
        // ONLY WHEN SCREEN TURNS ON
            // THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
        	 Bitmap bitmap = takeScreenshot();
        	 if(bitmap!=null)
		       saveBitmap(bitmap,"turnon");
		       Log.e("GREC", ScreenReceiver.wasScreenOn+"");
        
        super.onResume();
    }
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
